package com.ee.chat.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ee.chat.common.dao.UserImAnalysisMapper;
import com.ee.chat.common.entity.UserImAnalysis;
import com.ee.chat.common.service.IUserImAnalysisService;
import com.ndhutu.ee.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author AppiJ
 * @Date 2020/12/3 21:42
 */
@Service
public class UserImAnalysisServiceImpl implements IUserImAnalysisService {

    @Resource
    UserImAnalysisMapper imAnalysisMapper;

    @Override
    public void updateImAnalysis(Integer userId, String type) {

        Integer textNum = 0, pictureNum = 0, audioNum = 0, videoNum = 0, customNum = 0;
        switch (type) {
            case "TEXT":
                textNum += 1;
                break;
            case "PICTURE":
                pictureNum += 1;
                break;
            case "AUDIO":
                audioNum += 1;
                break;
            case "VIDEO":
                videoNum += 1;
                break;
            case "CUSTOM":
                customNum += 1;
                break;
        }

        String dateTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        QueryWrapper<UserImAnalysis> wrapper = new QueryWrapper<>();
        wrapper.eq("date_time", dateTime);
        wrapper.eq("user_id", userId);
        UserImAnalysis imAnalysis = imAnalysisMapper.selectOne(wrapper);

        if(imAnalysis == null){
            imAnalysis = new UserImAnalysis(dateTime, userId, textNum, pictureNum, audioNum, videoNum, customNum, 1);

            imAnalysisMapper.insert(imAnalysis);
        }else{
            imAnalysis.setTextNum(imAnalysis.getTextNum() + textNum);
            imAnalysis.setPictureNum(imAnalysis.getPictureNum() + pictureNum);
            imAnalysis.setAudioNum(imAnalysis.getAudioNum() + audioNum);
            imAnalysis.setVideoNum(imAnalysis.getVideoNum() + videoNum);
            imAnalysis.setCustomNum(imAnalysis.getCustomNum() + customNum);
            imAnalysis.setTotalNum(imAnalysis.getTotalNum() + 1);

            imAnalysisMapper.updateById(imAnalysis);
        }

    }
}
